package com.xiaochen.manager.service.impl;

import cn.hutool.core.date.DateUtil;
import com.xiaochen.manager.mapper.OrderInfoMapper;
import com.xiaochen.manager.mapper.OrderStatisticsMapper;
import com.xiaochen.manager.service.OrderInfoService;
import com.xiaochen.model.dto.order.OrderStatisticsDto;
import com.xiaochen.model.entity.order.OrderStatistics;
import com.xiaochen.model.vo.order.OrderStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

// -------------------------------这两种方式都可以正常使用-------------------------------------------
    /**
     * 查询统计每日的订单总数和金额
     * @param orderStatisticsDto
     * @return
     *
     * private List<String> dateList ;
     */
    // 方法一:使用for循环

    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        // 1.查询OrderStatics表里面的数据
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectList(orderStatisticsDto);
        // 2.将对应的金额和日期放入OrderStatisticsVo
        // 2.1创建两个集合一个放日期一个放金额
        List<String> dateList = new ArrayList<>();
        List<BigDecimal> amountList = new ArrayList<>();
        // 2.2遍历集合,将数据放入对应的集合
        for (OrderStatistics orderStatistics : orderStatisticsList){
            String formattedDate = DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd");
            dateList.add(formattedDate);
            amountList.add(orderStatistics.getTotalAmount());
        }
        // 2.3创建OrderStatisticsVo对象,将两个集合放入
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(amountList);

        // 3.返回结果
        return orderStatisticsVo;
    }
    /**
     * 方法二：使用stream流
     */
    /*@Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {

        // 查询统计结果数据
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectList(orderStatisticsDto) ;

        //日期列表
        List<String> dateList = orderStatisticsList.stream()
                .map(orderStatistics -> DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd"))
                .collect(Collectors.toList());

        //统计金额列表
        List<BigDecimal> amountList = orderStatisticsList.stream()
                .map(OrderStatistics::getTotalAmount)
                .collect(Collectors.toList());

        // 创建OrderStatisticsVo对象封装响应结果数据
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo() ;
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(amountList);

        // 返回数据
        return orderStatisticsVo;
    }*/

// --------------------------------------------------------------------
}


